﻿using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
using Riba.MVVMSL;

namespace TestGUI
{
	public class MainPageViewModel : ViewModelBase
	{
		public RelayCommand CommandShowMessageBox { get; private set; }
		public RelayCommand CommandOpenFileDialog { get; private set; }
		public RelayCommand CommandLoadChildWindow { get; private set; }
		public RelayCommand CommandNavigateAutoSwap { get; private set; }
		public RelayCommand CommandNavigateToView { get; private set; }
		public RelayCommand CommandNavigateToDialog1 { get; private set; }
		public RelayCommand CommandNavigateClear { get; private set; }
		public RelayCommand CommandDE { get; private set; }
		public RelayCommand CommandEN { get; private set; }

		private string _dataFromChildWindow;
		public string DataFromChildWindow
		{
			get { return _dataFromChildWindow; }
			set { SetPropertyValue(ref _dataFromChildWindow, value, () => DataFromChildWindow); }
		}

		private string _notificationFromPage3;
		public string NotificationFromPage3
		{
			get { return _notificationFromPage3; }
			set { SetPropertyValue(ref _notificationFromPage3, value, () => NotificationFromPage3); }
		}

		private string _status;
		public string Status
		{
			get { return _status; }
			set { SetPropertyValue(ref _status, value, () => Status); }
		}

		public MainPageViewModel()
		{
			NotificationFromPage3 = "-";
			DataFromChildWindow = "-";

			CommandShowMessageBox = new RelayCommand
				(
					handler =>
						RaiseShowMsgBox(new ShowMsgBoxEventArgs("Dieser Text soll angezeigt werden", "Titel der Message Box", MessageBoxButton.OKCancel, MessageBoxCallBack))
				) { IsEnabled = true };

			CommandOpenFileDialog = new RelayCommand
				(
					handler =>
						RaiseOpenFileDialog(new OpenFileDialogEventArgs("JPG Files (*.jpg;*.png)|*.jpg;*.jpg | All Files (*.*)|*.*", 1, true, OpenFileDialogCallBack))
				) { IsEnabled = true };

			CommandLoadChildWindow = new RelayCommand(OnLoadView) { IsEnabled = true };

			CommandNavigateAutoSwap = new RelayCommand(OnNavigateAutoSwap) { IsEnabled = true };

			CommandNavigateToView = new RelayCommand(OnNavigatToView) { IsEnabled = true };

			CommandNavigateToDialog1 = new RelayCommand
				(
					handler =>
						RaiseNavigateToView(new NavigateToViewEventArgs(typeof(Page1), AnimationMode.Forward))
				) { IsEnabled = true };

			CommandNavigateClear = new RelayCommand
				(
					handler => RaiseNavigateToView(new NavigateToViewEventArgs(null))){IsEnabled = true};
			
			CommandDE = new RelayCommand
				(
					handler =>
						{
							Style st = null;
							if (Application.Current.Resources.Contains("ChildWindowStyle"))
								st = Application.Current.Resources["ChildWindowStyle"] as Style;
							var buttons = new List<MessageWindowButton>();
							buttons.Add(new MessageWindowButton { Caption = "Abbrechen", Tag = "cancel"});
							buttons.Add(new MessageWindowButton { Caption = "OK", Tag = "ok"});
							var eventArgs = new ShowMessageWindowEventArgs("Dies ist eine Meldung", "Titel abcdefghijklmno pqrst uvwxyz", buttons, MBox_Callback, st);
							RaiseShowMessageWindow(eventArgs);
						}
				) { IsEnabled = true };

			CommandEN = new RelayCommand
				(
					handler =>
						{
							Style st = null;
							if (Application.Current.Resources.Contains("ChildWindowStyle"))
								st = Application.Current.Resources["ChildWindowStyle"] as Style;
							var buttons = new List<MessageWindowButton>();
							buttons.Add(new MessageWindowButton { Caption = "Cancel" });
							buttons.Add(new MessageWindowButton { Caption = "OK" });
							var eventArgs = new ShowMessageWindowEventArgs("This is a very long long message. 111 222 333333 444 555555 666666 77777 8888888 99999999 000000000 aaaaaa bbbbbbbb ccccccccc ddddd eeee fff gggggggggg hhhh iiiiiiiiiiiiii jjjjjjjjjjj kkkkkkkkk llllllll mmmmmmm nnnnnnnnnnnn oooooooooooo pppppp qqqqq rrrrrrrr ssssss ttttt uuuuuuu vvvvvv wwww xxxxxxx yyyyyyyy zzzzzzzzz ++++++\n\nNeue Zeile", "Title", buttons, MBox_Callback, st);
							RaiseShowMessageWindow(eventArgs);
						}
				) { IsEnabled = true };

			Notifier.Notify += Notifier_Notify;
		}

		private static void MBox_Callback(object tag)
		{
			var t = tag;
		}

		void Notifier_Notify(object sender, Notifier.NotificationEventArgs e)
		{
			if (e.Tag == "Notification from Page3ViewModel")
			{
				NotificationFromPage3 = e.Data.ToString();

				if (e.Callback != null)
					e.Callback(1);
			}
		}


		private static void MessageBoxCallBack(MessageBoxResult result)
		{
			Debug.WriteLine(result.ToString());
		}

		private static void OpenFileDialogCallBack(IEnumerable<FileInfo> result)
		{
			foreach (var fileInfo in result)
			{
				Debug.WriteLine(fileInfo.Name);
			}
		}

		private void OnLoadView(object handler)
		{
			RaiseLoadChildWindow(new LoadChildWindowEventArgs(typeof(ChildWindow1), DataFromChildWindow, OnLoadViewCallback));
		}

		public void OnLoadViewCallback(ViewModelBase vm, object data)
		{
			DataFromChildWindow = data.ToString();
		}

		private void OnNavigateAutoSwap(object handler)
		{
			RaiseNavigateAutoSwap();
		}

		private void OnNavigatToView(object handler)
		{
			const AnimationMode mode = AnimationMode.Forward;

			if (ExchangeDataDictionary["CurrentPage"] == typeof(Page1))
				RaiseNavigateToView(new NavigateToViewEventArgs(typeof(Page2), mode));
			else if (ExchangeDataDictionary["CurrentPage"] == typeof(Page2))
				RaiseNavigateToView(new NavigateToViewEventArgs(typeof(Page3), mode));
			else
				RaiseNavigateToView(new NavigateToViewEventArgs(typeof(Page1), mode));
		}
	}
}